package com.android.smsmessing.adapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.provider.Contacts.People;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.android.smsmessing.R;
import com.android.smsmessing.model.PeopleVO;

public class PeoplesAdapter extends BaseAdapter {

	private Context context;
	private LinearLayout peopleLineLayout;
	private EditText headTxtSearch;

	
	public PeoplesAdapter(Context c) {
		this.context = c;
		mInflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private ArrayList<PeopleVO> peoples = null;
	private LayoutInflater mInflater = null;

	@Override
	public int getCount() {
		if (peoples != null) {
			return peoples.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		ImageView imgCheck;
		TextView txtName;
		TextView txtMobile;
		TableLayout peopleTableLayout;
		PeopleVO people;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_people, null);
			holder.imgCheck = (ImageView) convertView
					.findViewById(R.id.imgCheck);
			holder.txtName = (TextView) convertView
					.findViewById(R.id.txtPeopleName);
			holder.txtMobile = (TextView) convertView
					.findViewById(R.id.txtPeopleMobile);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		PeopleVO currentPeople = peoples.get(position);
		holder.txtName.setText(currentPeople.name);
		holder.txtMobile.setText(currentPeople.mobile);
		holder.people = currentPeople;
		convertView.setTag(holder);
//		convertView.setOnClickListener(viewOnClick);
		
		return convertView;
	}

//	View.OnClickListener viewOnClick = new View.OnClickListener() {
//
//		@Override
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			v.setSelected(true);
//			ImageView imgView = (ImageView) v.findViewById(R.id.imgCheck);
//			ViewHolder holder = (ViewHolder) v.getTag();
//			PeopleVO people = holder.people;
//			if (people.isSelect) {
//				imgView.setImageResource(android.R.drawable.checkbox_off_background);
//			} else {
//				imgView.setImageResource(android.R.drawable.checkbox_on_background);
//			}
//			people.isSelect = !people.isSelect;
//		}
//
//	};

	public ArrayList<PeopleVO> getPeoples() {
		return peoples;
	}

	public void setPeoples(ArrayList<PeopleVO> peoples) {
		this.peoples = peoples;
		notifyDataSetChanged();
	}

	public void addPeople(PeopleVO people) {
		if (peoples == null) {
			peoples = new ArrayList();
		}
		peoples.add(people);
	}

}
